﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.ComponentModel;

using ShootSearch.Util;

namespace ShootSearch.Common
{
    #region Rule
    /// <summary>
    /// A serialized regex
    /// </summary>
    [TypeConverter(typeof(ExpandableConverter<Rule>))]
    [Serializable]
    public class Rule
    {
        /// <summary>
        /// Gets or sets the name of the rule
        /// </summary>
        [XmlAttribute]
        public string Name { get; set; }

        private RegexOptions options = RegexOptions.None;
        /// <summary>
        /// Gets or sets the regex options
        /// </summary>
        public RegexOptions Options
        {
            get { return options; }
            set
            {
                options = value;
                Pattern = pattern;
            }

        }

        protected string pattern;
        /// <summary>
        /// Gets or sets the regex pattern
        /// </summary>
        public string Pattern
        {
            get { return pattern; }
            set
            {
                Regex = Util.Tools.CreateRegex(value, options);
                if (Regex != null)
                    pattern = value;
            }
        }


        /// <summary>
        /// Gets the regex
        /// </summary>
        [XmlIgnore]
        public Regex Regex { get; protected set; }

        #region Constructors
        public Rule()
        {
        }

        public Rule(string pattern)
        {
            Pattern = pattern;
        }

        public Rule(string pattern, RegexOptions options)
        {
            Options = options;
            Pattern = pattern;
        }
        #endregion

        /// <summary>
        /// Regex.Match
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Match Match(string input)
        {
            if (Regex != null)
            {
                return this.Regex.Match(input);
            }
            else return null;
        }

        /// <summary>
        /// Regex.IsMatch
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool IsMatch(string input)
        {
            if (Regex != null)
            {
                return this.Regex.IsMatch(input);
            }
            else return false;
        }

        /// <summary>
        /// Regex.Matches
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public MatchCollection Matches(string input)
        {
            if (Regex != null)
            {
                return this.Regex.Matches(input);
            }
            else return null;
        }

        /// <summary>
        /// Regex.Replace
        /// </summary>
        /// <param name="input"></param>
        /// <param name="replacement"></param>
        /// <returns></returns>
        public string Replace(string input, string replacement)
        {
            if (Regex != null)
            {
                return this.Regex.Replace(input, replacement);
            }
            else return input;
        }

        /// <summary>
        /// Get a string summary
        /// </summary>
        /// <returns>string summary</returns>
        public override string ToString()
        {
            return pattern;
        }
    } 
    #endregion
}
